package com.wuye.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuye.VO.WyYbMoneyVo;
import com.wuye.basic.domain.WyCommunity;
import com.wuye.basic.domain.WyHome;
import com.wuye.basic.mapper.WyCommunityMapper;
import com.wuye.basic.mapper.WyHomeMapper;
import com.wuye.domain.WyYbType;
import com.wuye.domain.WyYibiao;
import com.wuye.dto.WyYbMoneyDto;
import com.wuye.mapper.WyYbTypeMapper;
import com.wuye.mapper.WyYibiaoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wuye.mapper.WyYbMoneyMapper;
import com.wuye.domain.WyYbMoney;
import com.wuye.service.IWyYbMoneyService;

/**
 * 仪表-----周期收费Service业务层处理
 * 
 * @author jack
 * @date 2024-11-22
 */
@Service
public class WyYbMoneyServiceImpl extends ServiceImpl<WyYbMoneyMapper,WyYbMoney> implements IWyYbMoneyService
{
    @Autowired
    private WyYbMoneyMapper wyYbMoneyMapper;

    @Autowired
    private WyCommunityMapper wyCommunityMapper;

    @Autowired
    private WyHomeMapper wyHomeMapper;

    @Autowired
    private WyYibiaoMapper wyYibiaoMapper;

    @Autowired
    private WyYbTypeMapper wyYbTypeMapper;

    /**
     * 查询仪表-----周期收费
     * 
     * @param ybMoyId 仪表-----周期收费主键
     * @return 仪表-----周期收费
     */
    @Override
    public WyYbMoney selectWyYbMoneyByYbMoyId(Long ybMoyId)
    {
        return wyYbMoneyMapper.selectWyYbMoneyByYbMoyId(ybMoyId);
    }

    /**
     * 查询仪表-----周期收费列表
     * 
     * @param wyYbMoneyDto 仪表-----周期收费
     * @return 仪表-----周期收费
     */
    @Override
    public List<WyYbMoneyVo> selectWyYbMoneyList(WyYbMoneyDto wyYbMoneyDto)
    {
        QueryWrapper<WyYibiao> wrapper = new QueryWrapper<>();
        if (wyYbMoneyDto.getYbName() !=null && wyYbMoneyDto.getYbName() !=""){
            wrapper.like("yb_name", wyYbMoneyDto.getYbName());
        }

        if (wyYbMoneyDto.getCommunityId() !=null){
            wrapper.eq("community_id", wyYbMoneyDto.getCommunityId());
        }
        if (wyYbMoneyDto.getTypeId() !=null ){
            wrapper.like("yb_type_id", wyYbMoneyDto.getTypeId());
        }
        ArrayList<WyYbMoneyVo> wyYbMoneyVos = new ArrayList<>();

        List<WyYibiao> wyYibiaos = wyYibiaoMapper.selectList(wrapper);
        if (wyYibiaos.isEmpty()){
            return wyYbMoneyVos;
        }
        List<WyYbMoney> wyYbMonies = new ArrayList<>();
        for (WyYibiao wyYibiao : wyYibiaos){
            List<WyYbMoney> wyYbMoneyList=wyYbMoneyMapper.selectByYibiaoId(wyYibiao.getYbId());
            wyYbMonies.addAll(wyYbMoneyList);
        }
        if (wyYbMonies.isEmpty()){
            return wyYbMoneyVos;
        }

        for (WyYbMoney wyYbMoney : wyYbMonies){
            WyYbMoneyVo wyYbMoneyVo = new WyYbMoneyVo();
            WyYibiao wyYibiao = wyYibiaoMapper.selectById(wyYbMoney.getYbId());
//            if (wyYibiao.getDel()==1){
//                break;
//            }
            WyCommunity wyCommunity = wyCommunityMapper.selectById(wyYibiao.getCommunityId());
            WyHome wyHome = wyHomeMapper.selectById(wyYibiao.getHouseId());
            WyYbType wyYbType = wyYbTypeMapper.selectById(wyYibiao.getYbTypeId());
            BeanUtils.copyProperties(wyYbMoney, wyYbMoneyVo);
            wyYbMoneyVo.setWyCommunity(wyCommunity);
            wyYbMoneyVo.setWyHome(wyHome);
            wyYbMoneyVo.setWyYbType(wyYbType);
            wyYbMoneyVos.add(wyYbMoneyVo);
        }

        return wyYbMoneyVos;
    }

    /**
     * 新增仪表-----周期收费
     * 
     * @param wyYbMoney 仪表-----周期收费
     * @return 结果
     */
    @Override
    public int insertWyYbMoney(WyYbMoney wyYbMoney)
    {
        return wyYbMoneyMapper.insertWyYbMoney(wyYbMoney);
    }

    /**
     * 修改仪表-----周期收费
     * 
     * @param wyYbMoney 仪表-----周期收费
     * @return 结果
     */
    @Override
    public int updateWyYbMoney(WyYbMoney wyYbMoney)
    {
        return wyYbMoneyMapper.updateWyYbMoney(wyYbMoney);
    }

    /**
     * 批量删除仪表-----周期收费
     * 
     * @param ybMoyIds 需要删除的仪表-----周期收费主键
     * @return 结果
     */
    @Override
    public int deleteWyYbMoneyByYbMoyIds(Long[] ybMoyIds)
    {
        return wyYbMoneyMapper.deleteWyYbMoneyByYbMoyIds(ybMoyIds);
    }

    /**
     * 删除仪表-----周期收费信息
     * 
     * @param ybMoyId 仪表-----周期收费主键
     * @return 结果
     */
    @Override
    public int deleteWyYbMoneyByYbMoyId(Long ybMoyId)
    {
        return wyYbMoneyMapper.deleteWyYbMoneyByYbMoyId(ybMoyId);
    }

    @Override
    public int updateWyYbMoney2(WyYbMoney wyYbMoney) {
        BigDecimal money = wyYbMoney.getYbMoyMoney();
        int num = money.compareTo(new BigDecimal(0));
        if (num != 0 && wyYbMoney.getYbMoyStatus()==0){
            return -1;
        }
        wyYbMoney.setYbMoyBeginElectricity(new BigDecimal(0));
        wyYbMoney.setYbMoyEndElectricity(new BigDecimal(0));
        wyYbMoney.setYbMoyBeginTime(new Date());
        wyYbMoney.setYbMoyEndTime(new Date());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        wyYbMoney.setYbMoyRemark("换新时间为:"+format);
        wyYbMoney.setYbMoyCreateTime(new Date());
        wyYbMoney.setYbMoyMoney(new BigDecimal(0));
        return wyYbMoneyMapper.updateById(wyYbMoney);
    }
}
